package org.swimv2.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;


/**
 * The persistent class for the User database table.
 * 
 * @author Adriano Gaibotti
 * @author Lorenzo Delledonne
 */
@NamedQueries({
	@NamedQuery(
		name="User.getUserById",
		query="SELECT u FROM User AS u WHERE u.id = :id"),
	@NamedQuery(
		name="User.getUserByEmailAndPassword",
		query="SELECT u FROM User AS u WHERE u.eMail = :email AND u.password = :password"),
	@NamedQuery(
		name="User.getUserByEmail",
		query="SELECT u FROM User AS u WHERE u.eMail = :email"),
	@NamedQuery(
		name="User.searchForName",
		query="SELECT u FROM User AS u WHERE u.firstName = :name OR u.lastName = :name ORDER BY u.lastName ASC, u.firstName DESC"),
	@NamedQuery(
		name="User.searchForSkill",
		query="SELECT u FROM User AS u JOIN u.skills AS s WHERE s = :skill ORDER BY u.lastName ASC, u.firstName DESC"),
	@NamedQuery(
			name="User.searchUserNotFriend",
			query="SELECT us FROM User AS us WHERE us NOT IN (SELECT u FROM User AS u1, IN(u1.friend1) u WHERE u1 = :user) " +
					"AND us NOT IN (SELECT u FROM User AS u1, IN(u1.friend2) u WHERE u1 = :user)")
})
@Entity
public class User implements Serializable {
	private static final long serialVersionUID = 5081851654912033952L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;

	@Column(name="e_mail")
	private String eMail;

	@Column(name="first_name")
	private String firstName;

	@Column(name="is_admin")
	private boolean isAdmin;

	@Column(name="last_name")
	private String lastName;

	private String password;

	private String place;

	//uni-directional many-to-many association to Skill
    @ManyToMany(cascade=CascadeType.ALL)
	@JoinTable(
		name="user_has_skill"
		, joinColumns={
			@JoinColumn(name="user_id")
			}
		, inverseJoinColumns={
			@JoinColumn(name="skill_id")
			}
		)
	private Set<Skill> skills;

	//bi-directional many-to-many association to User
    @ManyToMany(cascade=CascadeType.ALL)
	@JoinTable(
		name="friendship"
		, joinColumns={
			@JoinColumn(name="user_id1")
			}
		, inverseJoinColumns={
			@JoinColumn(name="user_id")
			}
		)
	private Set<User> friend1;

	//bi-directional many-to-many association to User
	@ManyToMany(mappedBy="friend1")
	private Set<User> friend2;

	// constructor
    public User() {
    }

    ////////////////////////
    // getters and setters//
    ////////////////////////
    
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getEMail() {
		return this.eMail;
	}

	public void setEMail(String eMail) {
		this.eMail = eMail;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public boolean getIsAdmin() {
		return this.isAdmin;
	}

	public void setIsAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPlace() {
		return this.place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public Set<Skill> getSkills() {
		return this.skills;
	}

	public void setSkills(Set<Skill> skills) {
		this.skills = skills;
	}
	
	public Set<User> getFriend1() {
		return this.friend1;
	}

	public void setFriend1(Set<User> friend1) {
		this.friend1 = friend1;
	}
	
	public Set<User> getFriend2() {
		return this.friend2;
	}

	public void setFriend2(Set<User> friend2) {
		this.friend2 = friend2;
	}
	
}